﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the API Concepts Framework
 * http://apiconceptsframework.codeplex.com
 *
 * Copyright (C) 2014 eva Kühn, Thomas Scheller, Vienna University of Technology
 * http://www.complang.tuwien.ac.at/eva
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 *
 * ---------------------------------------------------------------- */
#endregion

using System.Reflection;
using ApiConceptsFramework.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ApiConceptsFramework.HCs
{
	public class InformationLookup: IHighLevelConcept
	{
		public const float ExternalInformationLookupComplexity = 8;

		private readonly LearningEffect _informationLookupLearningEffect = new LearningEffect(0.5f, 2, 0.5f);

		public float CalculateCost(UsedItem item)
		{
			return CalculateCost(item.NumberOfUsages, item.PreviousUsages);
		}

		public float CalculateCost(int numberOfUsages, int previousUsages)
		{
			return _informationLookupLearningEffect.CalculateComplexityForMultipleUsages(ExternalInformationLookupComplexity,
			                                                                             numberOfUsages, previousUsages);
		}

		public Type InputDataType { get { return typeof (InformationLookupInputData); } }

		public HcEvaluationResult Evaluate(List<Assembly> apiAssemblies, IEnumerable<ConceptInstance> data, IHcEvaluationInput hcInputData,
		                                        IEnumerable<HcEvaluationResult> otherHcData, IDE ide = IDE.VisualStudioOrSimilar)
		{
			if (hcInputData == null)
				return null;
			var pd = hcInputData as InformationLookupInputData;
			if (pd == null)
				throw new ArgumentException("hcInputData must be InformationLookupInputData", "hcInputData");

			if (pd.ConceptsRequiringLookup.Count == 0)
				return null;

			CalculateComplexityForConceptsWithExternalLookup(pd.ConceptsRequiringLookup);
			return new HcEvaluationResult{HighLevelConcept = this, GeneralDescription = "Some concepts require lookup of external information - this should be prevented where possible."};
		}

		private void CalculateComplexityForConceptsWithExternalLookup(IEnumerable<ConceptWithLookup> conceptsRequiringExternalClassLookup)
		{
			foreach (var c in conceptsRequiringExternalClassLookup)
			{
				c.Concept.HcData.Add(new HcComplexityData
				{
					HighLevelConcept = this,
					UsageComplexity = CalculateCost(c.Concept) * c.NumberOfLookups,
					Description = "lookup of external information (" + c.NumberOfLookups + " lookups)"
				});
			}
		}
	}

	public class ConceptWithLookup
	{
		public ConceptInstance Concept { get; set; }
		public int NumberOfLookups { get; set; }
	}

	public class InformationLookupInputData : IHcEvaluationInput
	{
		public List<ConceptWithLookup> ConceptsRequiringLookup { get; private set; }

		public InformationLookupInputData()
		{
			ConceptsRequiringLookup = new List<ConceptWithLookup>();
		}
	}
}
